package com.itheima.health.service.impl;

import com.itheima.health.dao.MemberDao;
import com.itheima.health.pojo.Member;
import com.itheima.health.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

@Service
public class MemberServicelmpl implements MemberService {
    @Autowired
    private MemberDao memberDao;

    /**
     * 根据月份查数据库
     *
     * @return
     */
    @Override
    public List<Integer> findMemberCountByMonths(List<String> month) {
        Calendar calendar = Calendar.getInstance();
        List<Integer> list = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        for (String m : month) {
            String[] split = m.split("-");
            //设置年
            calendar.set(Calendar.YEAR, Integer.parseInt(split[0]));
            //设置月
            calendar.set(Calendar.MONTH, Integer.parseInt(split[1]));
            //设置月的最后一天
            calendar.set(Calendar.DAY_OF_MONTH, 0);
            String date = sdf.format(calendar.getTime());
            Integer count = memberDao.findMemberCountBeforeDate(date);
            list.add(count);
        }
        return list;
    }

    /**
     * 根据id查member  任务11新增
     * @param memberId
     * @return
     */
    @Override
    public Member findById(Integer id) {
        return memberDao.findById(id);
    }
}